﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BasicData.Clients;
using WMS.BasicData.ProductDetails.DTO;
using WMS.Common;


namespace WMS.BasicData.ProductDetails
{
    public class ProductDetailsService : ApplicationService, IProductDetailsService
    {
        private readonly IRepository<ProductDetail,int> repository;

        private readonly IRepository<Client> clientRepository;

        public ProductDetailsService(IRepository<ProductDetail,int> repository, IRepository<Client> clientRepository)
        {
            this.repository = repository;
            this.clientRepository = clientRepository;
        }


        /// <summary>
        /// 分页查询商品明细
        /// </summary>
        public async Task<PagedResult> ShowProductDetails(int page, int limit, int belongsOwner, string? productName)
        {
            //获取并等待仓库储位定义列表
           var productlist=await repository.GetListAsync();
            //将列表转换为可查询对象
            var productDefinitionQueryable=productlist.AsQueryable();

             //获取并等待客户列表
            var clientlist = await clientRepository.GetListAsync();
            //将列表转换为可查询对象
            var clientDefinitionQueryable = clientlist.AsQueryable();


            var alllist = from a in productDefinitionQueryable
                          join b in clientDefinitionQueryable on a.BelongsOwner equals b.Id
                          select new TwoTableDTO
                          {
                              Id = a.Id,
                              ProductCode = a.ProductCode,
                              ProductName = a.ProductName,
                              BelongsOwner=a.BelongsOwner,
                              ProductCategories=a.ProductCategories,
                              Unit=a.Unit,
                              Price=a.Price,
                              ProductAttributes=a.ProductAttributes,
                              ItemBarcode = a.ItemBarcode,
                              ProductSpecifications=a.ProductSpecifications,
                              ProductBranding=a.ProductBranding,
                              SKU=a.SKU,
                              Weight = a.Weight,
                              Wide=a.Wide,
                              Long=a.Long,
                              High = a.High,
                              Volume=a.Volume,
                              ShelfLife=a.ShelfLife,
                              BillableItems=a.BillableItems,
                              CodeDiscNumber=a.CodeDiscNumber,
                              DiscLayerHeight=a.DiscLayerHeight,
                              TearDownControl=a.TearDownControl,
                              SplitUnits=a.SplitUnits,
                              SplitNumber=a.SplitNumber,
                              NumLnventoryLevels=a.NumLnventoryLevels,
                              ChineseName=b.ChineseName,
                          };

            alllist = alllist.WhereIf(belongsOwner != 0, m => m.BelongsOwner == belongsOwner);
            alllist = alllist.WhereIf(!string.IsNullOrEmpty(productName), m => m.ProductName.Contains(productName));

            #region 分页
            //var pagedData = alllist.Skip((page - 1) * limit).Take(limit);

            //int totalCount = alllist.Count();

            //PagedResult pagedResult = new PagedResult
            //{
            //    RowCount = totalCount,
            //    PageCount = (int)Math.Ceiling(totalCount / (double)limit),
            //    CurrentPage = page,
            //    Queryable = pagedData
            //};
            //return pagedResult;

            //var predicate =PredicateBuilder.New<ProductDetail>(true);
            //if (belongsOwner != 0)
            //{
            //    predicate = predicate.And(m => m.BelongsOwner == belongsOwner);
            //}
            //if (!string.IsNullOrEmpty(productName))
            //{
            //    predicate = predicate.And(m => m.ProductName.Contains(productName));
            //}

            //var productDetaillist = await repository.GetListAsync(predicate);

            #endregion
            PagedResult pagedResult = new PagedResult();
            pagedResult.RowCount = page;
            pagedResult.PageCount = limit;
            pagedResult.CurrentPage = alllist.Count();
            pagedResult.Queryable = alllist.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return pagedResult;
        }

        /// <summary>
        /// 创建商品明细
        /// </summary>
        public async Task<int> CreatePrdouctDetails(ProductDetail createProductDetailsDto)
        {
            var productlist = await repository.GetListAsync(m => m.ProductCode == createProductDetailsDto.ProductCode);
            if (productlist.Count() > 0) return -1;

            var product = createProductDetailsDto.MapTo<ProductDetail>();
            await repository.InsertAsync(product);
            return 1;
        }

        /// <summary>
        /// 删除商品明细
        /// </summary>
        public async Task<int> DeleteProductDetails(int id)
        {
            await repository.DeleteAsync(m => m.Id == id);
            return 1;
        }

        /// <summary>
        /// 查看商品明细
        /// </summary>
        /// <param name="id"></param>
        public async Task<CUProductDetailsDto> GetProductDetails(int id)
        {
           var productDetail =await repository.FirstOrDefaultAsync(m => m.Id == id);
            var productDetailDto = productDetail.MapTo<CUProductDetailsDto>();
            return productDetailDto;
        }

        /// <summary>
        /// 更新商品明细
        /// </summary>
        /// <param name="id"></param>
        public async Task<int> UpdateProductDetails(CUProductDetailsDto productDTO)
        {
           var productDetaillist=repository.GetListAsync(m => m.Id == productDTO.Id &&m.ProductCode==productDTO.ProductCode).Result;
            if (productDetaillist.Count() > 0)
                return -1;

            var productlist=productDTO.MapTo<ProductDetail>();
            await repository.UpdateAsync(productlist);
            return 1;
        }


        //绑定客户名称
        public async Task<List<Client>> BindClientName()
        {
            var client = PredicateBuilder.New<Client>(true);
            var clientlist = await clientRepository.GetListAsync(client);
           return clientlist;
        }


        //批量删除商品明细
        public async Task<int> BulkDeleteProductDetails(string array)
        {
            await repository.DeleteAsync(m => array.Contains(m.Id.ToString()));
            return 1;
        }


    }
}
